var currentPage = 1;//当前页码
var pageSize = 10;//每页显示的数据量
var totalCount = 0;//总条数
var totalPage = 1;//总页码

//加载分页信息
function showInfo() {
    let depart=$("#depart").val();
    if(depart=="人事部"){
        //清空
        $("#table-page tr:gt(0) td").html("&nbsp;");
        let json = {
            'jt_id': $("#jt_id").val().trim(),
            'usetime': $("#usetime").val(),
            'name': $("#name").val(),
            'state': $("#state").val(),
            'currentPage': currentPage,
            'pageSize': pageSize
        }
        console.log(json);
        $.ajaxSettings.async = false;
        $.getJSON("/sfc/sellCars", json, function (data) {
            console.log(data);
            //总条数
            totalCount = data.total;
            //总页数
            totalPage = data.pages;
            $(data.list).each(function (i, v) {
                let a = "";
                if (v.state == -1) {
                    a = "驳回";
                } else if (v.state == 0) {
                    a = "审核中";
                } else if (v.state == 1) {
                    a = "通过";
                }

                $("#table-page").find("tr").eq(i + 2).find("td").eq(0).text((currentPage - 1) * pageSize + i + 1);
                $("#table-page").find("tr").eq(i + 2).find("td").eq(1).html('<a href="javascript:void(0)" onclick="look(' + v.id + ')">' + v.name + '的申请</a>');
                $("#table-page").find("tr").eq(i + 2).find("td").eq(2).text(v.usetime);
                $("#table-page").find("tr").eq(i + 2).find("td").eq(3).text(a);
                $("#table-page").find("tr").eq(i + 2).find("td").eq(4).html("<button type='button' class='layui-btn layui-btn-sm' onclick='look(" + v.id + ")'>查&nbsp;&nbsp;看</button>");
            });

        });
        $.ajaxSettings.async = true;
        if (totalCount == 0) {
            totalPage=1;
            $(".showPage").text("第" + currentPage + "页 / 共" + totalPage + "页");
            layer.open({
                content: '未找到数据！',
                skin: 'layui-layer-demo',
                closeBtn: 1,
                title: "提示"
            })
        }
        //调用页数
        showPage()
    }else{
        layer.open({
            content: '您没有权限查看！',
            skin: 'layui-layer-demo',
            closeBtn: 1,
            title: "提示"
        })
    }

}

//展示页码
function showPage() {
    $(".showPage").text("第" + currentPage + "页 / 共" + totalPage + "页");
}

function next() {
    currentPage++;
    if (currentPage > totalPage) {
        currentPage = totalPage;
        return;
    }
    showInfo()
}

function prev() {
    currentPage--;
    if (currentPage < 1) {
        currentPage = 1;
        return;
    }
    showInfo()
}

function start() {
    currentPage = 1;
    showInfo()
}

function end() {
    currentPage = totalPage;
    showInfo()
};

function look(id) {
    let approver = $("#user").val();

    layer.open({
        type: 2,
        skin: 'layui-layer-demo',
        title: "查看",
        anim: 2,
        area: ['500px', '470px'],
        content: '/sfc/selCarbyid?id=' + id ,
        btn: ['通过', '驳回'],
        btnAlign: 'c',
        btn1: function (index1) {
            let state = 1;
            let json = {
                id: id,
                state: state,
                approver: approver
            }
            $.post("/sfc/updCar", json, function (data) {
                if (data == 1) {
                    layer.open({
                        content: '审核成功！',
                        skin: 'layui-layer-demo',
                        closeBtn: 1,

                    })
                    layer.close(index1);

                } else {
                    layer.open({
                        content: '审核失败！',
                        skin: 'layui-layer-demo',
                        closeBtn: 1
                    })
                    layer.close(index1);
                }
                showInfo();
            });
        },
        btn2: function (index2) {
            let state = -1;
            let json = {
                id: id,
                state: state,
                approver: approver
            }
            $.post("/sfc/updCar", json, function (data) {
                if (data == 1) {
                    layer.open({
                        content: '审核成功！',
                        skin: 'layui-layer-demo',
                        closeBtn: 1
                    })
                    layer.close(index2);

                } else {
                    layer.open({
                        content: '审核失败！',
                        skin: 'layui-layer-demo',
                        closeBtn: 1
                    })
                    layer.close(index2);
                }
                showInfo();
            });
        },

    });
}


